<?php

/* ----------------------------------------------
  Caloris: Module Pages Editor

  $Id$
  $Author$

  Editor stránek s WYSIWYG editorem
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'pageeditor');

$filen = $_GET["name"];

function tidyup($name) {
    $tidy_options = array(
        'input-xml' => true,
        'output-xml' => true,
        'indent' => true,
        'wrap' => false,
        'indent-cdata' => true,
    );

    $tidy = new tidy();
    $tidy->parseFile(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml', $tidy_options, 'utf8');
    $tidy->cleanRepair();

    $xmm = simplexml_load_string($tidy);
    $xmm->asXML(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml'); 
}

if ($_POST["function_name"] == 'page_properties') {
    if (Caloris\ACL::authorize("templates") == 1) {
        $file = _CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml';
        $xmr = new DOMDOcument;
        $xmr->load($file);

        // Obsah
        $xpath = new DOMXpath($xmr);

        // Skripty
        $scripts = explode(";", $_POST["scripts"]);
        $style = explode(";", $_POST["style"]);
        $rss = explode(";", $_POST["rssfeed"]);

        foreach ($xpath->query('//page[@id="' . $_POST["name"] .  '"]/scripts/files') as $node) {
            $node->parentNode->removeChild($node);
        }

        foreach ($xpath->query('//page[@id="' . $_POST["name"] .  '"]/style/files') as $node) {
            $node->parentNode->removeChild($node);
        }

        foreach ($xpath->query('//page[@id="' . $_POST["name"] .  '"]/rss/files') as $node) {
            $node->parentNode->removeChild($node);
        }

        $xmr->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

        $xmn = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');
        $xmg = $xmn->xpath('//page[@id="' . $_POST["name"] .  '"]');

        $xmg[0]->title = $_POST["posthead"];
        $xmg[0]->enable = Caloris\Tools::checknull($_POST["enable"]);
        $xmg[0]->sitemap = Caloris\Tools::checknull($_POST["sitemap"]);
        $xmg[0]->meta->description = $_POST["metadesc"];
        $xmg[0]->meta->keywords = $_POST["metakeys"];

        for ($a = 0; $a < count($scripts); $a++) {
            $xmg[0]->scripts->files[] = $scripts[$a];
        }

        for ($a = 0; $a < count($style); $a++) {
            $xmg[0]->style->files[] = $style[$a];
        }

        for ($a = 0; $a < count($rss); $a++) {
            $xmg[0]->rss->files[] = $rss[$a];
        }

        $xmn->asXML($file);
        
        tidyup($_POST["name"]);
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.properties.php?name=" . $_POST["name"] . "");
        exit();
    } else {
        header("location: " . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . "/pageeditor/pages.properties.php?name=" . $_POST["name"] . "&id=" . $_POST["name"] . "&msg=" . urlencode("Nemáte oprávnění"));
        exit();
    }
}

// Načtení informací z XML souboru

$xmr = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');
$xml = $xmr->xpath('//page[@id="' . $_GET["name"] .  '"]');

for ($a = 0; $a < count($xml[0]->style->files); $a++) {
    $styletext .= $xml[0]->style->files[$a] . ';';
}

for ($a = 0; $a < count($xml[0]->rss->files); $a++) {
    $rsstext .= $xml[0]->rss->files[$a] . ';';
}

for ($a = 0; $a < count($xml[0]->scripts->files); $a++) {
    $scripttext .= $xml[0]->scripts->files[$a] . ';';
}

$content .= '
<h1>Vlastnosti ' . $_REQUEST["name"] . '</h1>' . PHP_EOL;

if ($xml[0]->system == 1) {
    $system = 'Systémový soubor (nelze smazat z těchto stránek)';
} else {
    $system = 'Uživatelský soubor';
}

if ($xml[0]->enable == 1) {
    $enable = 'ano <input type="radio" name="enable" value="1" checked="checked"  /> ne <input type="radio" name="enable" value="0" />';
} else {
    $enable = 'ano <input type="radio" name="enable" value="1"/> Ne <input type="radio" name="enable" value="0" checked="checked" />';
}

if ($xml[0]->sitemap == 1) {
    $sitemap = 'ano <input type="radio" name="sitemap" value="1" checked="checked"  /> ne <input type="radio" name="sitemap" value="0" />';
} else {
    $sitemap = 'ano <input type="radio" name="sitemap" value="1"/> ne <input type="radio" name="itemap" value="0" checked="checked" />';
}

$content .= '
<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/pages.properties.php" method="post">
<div class="control-group">
<label class="control-label">Název stránky</label>
<div class="controls"><input type="text" name="posthead" value="' . Caloris\Text::caltag($xml[0]->title) . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">SEO název</label>
<div class="controls"><input type="hidden" name="function_name" value="page_properties" /><input type="text" name="name" value="' . $_REQUEST["name"] . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Systém</label>
<div class="controls">' . $system . '</div>
</div>

<div class="control-group">
<label class="control-label">Povolit soubor</label>
<div class="controls">' . $enable . '</div>
</div>

<div class="control-group">
<label class="control-label">Zahrnout stránku v sitemap.xml</label>
<div class="controls">' . $sitemap . '</div>
</div>

<div class="control-group">
<label class="control-label">Externí styl</label>
<div class="controls"><input type="text" name="style" value="' . substr($styletext, 0, -1) . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">RSS</label>
<div class="controls"><input type="text" name="rssfeed" value="' . substr($rsstext, 0, -1) . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Meta hesla</label>
<div class="controls"><input type="text" name="metakeys" value="' . $xml[0]->meta->keywords . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Meta popisky</label>
<div class="controls"><input type="text" name="metadesc" value="' . $xml[0]->meta->description . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Javaskripty</label>
<div class="controls"><input type="text" name="scripts" value="' . substr($scripttext, 0, -1) . '" class="input-xxlarge" /></div>
</div>

<div class="control-group">
<label class="control-label">Modul (pokud patří stránka do modulu)</label>
<div class="controls">' . $xml[0]->module . '</div>
</div>

<div class="control-group">
<div class="controls"><input type="submit" value="Uložit" class="btn btn-primary" /></div>
</form>
<br />';

$data["title"] = 'Stránky: Editor';

$tmpl = new Caloris\Template;
$tmpl->input(array($content));
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');